`include "PRV564Config.v"
`include "PRV564Define.v"
//////////////////////////////////////////////////////////////////////////////////////////////////
//  Date    : 2021/7/5                                                                          //
//  Author  : Jack.Pan                                                                          //
//  Desc    : Page check unit for PRV564 processor                                              //
//  Version : 0.0(Orignal)                                                                      //
//////////////////////////////////////////////////////////////////////////////////////////////////
module PageCheck(
//csr
    input wire [1:0]CSR_priv,           //CPU privlage input
    input wire      CSR_mxr,            //CSR MXR bit input
    input wire      CSR_sum,            //CSR SUM bit input
//read or write
    input wire      OP_read,            //operations
    input wire      OP_write,
    input wire      OP_execute,

//PTE
    input wire      PTE_U,
    input wire      PTE_W,
    input wire      PTE_R,
    input wire      PTE_X,
    input wire      PTE_D,

//检查正确
    output wire     check_ok

);

//权限检查
wire privilege_check_ok;

//读检查
wire read_check;
//写检查
wire write_check;
//执行检查
wire execute_check;

assign privilege_check_ok	=	((CSR_priv==`User) & PTE_U) | 		//U模式访问U模式的页面
								((CSR_priv==`Supervisior) & PTE_U & CSR_sum) | 	//S模式在SUM=1访问U模式
								(CSR_priv==`Supervisior) & !PTE_U | 			
								(CSR_priv==`Machine);
								
assign read_check	=	OP_read & PTE_R | OP_read & PTE_X & CSR_mxr;
assign write_check	=	OP_write & PTE_W;
assign execute_check=	OP_execute & PTE_X;

assign check_ok	=	privilege_check_ok & (read_check | write_check | execute_check);

endmodule
